﻿using BDPAutodetect.Device;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDPAutodetect.Client.ViewWindows.WindowDialog
{
    /// <summary>
    /// SettingTargetEditorDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class SettingTargetEditorDialog : Window, IDialog<MTarget>
    {
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 查询设备类型集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<DeviceTypeData> deviceTypeItems;
        /// <summary>
        /// 当前编辑参数界限数据
        /// </summary>
        [ObservableProperty]
        protected MTarget editorData;
        /// <summary>
        /// 源数据
        /// </summary>
        protected MTarget SourceData { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public MTarget? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingTargetEditorDialog(MTarget argTargetData, ObservableCollection<DeviceTypeData> argDeviceTypeDatas)
        {
            InitializeComponent();
            this.DeviceTypeItems = argDeviceTypeDatas;
            this.EditorData = argTargetData.CloneDeep();
            this.SourceData = argTargetData;
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (this.EditorData.Name.Equals(this.SourceData.Name, StringComparison.OrdinalIgnoreCase))
            {
                this.StatusMsg = "未调整，可以直接点击取消";
                return;
            }
            this.DialogResult = true;
            this.DialogResultData = this.EditorData;
            if (this.EditorData.TargetCategoryData != null)
            {
                this.DialogResultData.TargetCategory = this.EditorData.TargetCategoryData.DeviceType;
            }
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
